package cn.zahoyuening.hnistalumnirecord;

import cn.zhaoyuening.hnistalumnirecord.domain.News;
import cn.zhaoyuening.hnistalumnirecord.domain.NewsModule;
import cn.zhaoyuening.hnistalumnirecord.service.AdminUserService;
import cn.zhaoyuening.hnistalumnirecord.service.NewsModuleService;
import cn.zhaoyuening.hnistalumnirecord.service.NewsService;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import sun.security.pkcs11.Secmod;

import java.util.Date;

/**
 * Created by zhao on 17-1-29.
 */
public class InitData {
    private ApplicationContext rootContext =
            new ClassPathXmlApplicationContext("spring/root-context.xml");
    /*    private ApplicationContext webContext =
                new ClassPathXmlApplicationContext("spring/web-context.xml");*/
    private NewsModuleService moduleService = rootContext.getBean(NewsModuleService.class);
    private NewsService newsService = rootContext.getBean(NewsService.class);
    private AdminUserService adminUserService = rootContext.getBean(AdminUserService.class);
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Test
    public void initModule() {
        NewsModule module1 = new NewsModule("母校动态");
        module1.setId(1);
        NewsModule module2 = new NewsModule("校友消息");
        module2.setId(2);
        NewsModule module3 = new NewsModule("杰出校友");
        module3.setId(3);
        NewsModule module4 = new NewsModule("理工相册");
        module4.setId(4);
        NewsModule module5 = new NewsModule("通知公告");
        module5.setId(5);
        NewsModule module6 = new NewsModule("校友捐赠明细");
        module6.setId(6);
        NewsModule module7 = new NewsModule("校友捐赠方式");
        module7.setId(7);
        moduleService.save(module1);
        moduleService.save(module2);
        moduleService.save(module3);
        moduleService.save(module4);
        moduleService.save(module5);
        moduleService.save(module6);
        moduleService.save(module7);
    }

    @Test
    public void addTestNewsData() throws Exception {
        NewsModule module = new NewsModule();
        module.setId(3);
        long l = new Date().getTime();
        long t= 86400000l;
        for (int i =0;i<100;i++){
            News news = new News();
            news.setTitle("校领导走访慰问离退休老同志"+i);
            news.setContent("<p>haha</p>");
            news.setThumbnailPath("/resource/uploadImg/5a3540b6-ff30-4ed5-9380-8f0189fa6ff0");
            news.setIntrduce("balabala");
            news.setAuthor("zhao");
            news.setDate(new Date(l+(i*t)));
            news.setModule(module);
            newsService.save(news);
        }

    }

    @Test
    public void initNews() throws Exception {
        News news = newsService.findById(4);
        for (int i=1;i<=20;i++){
            News news1 = new News();
            news1.setAuthor(news.getAuthor());
            news1.setId(i);
            news1.setIntrduce(news.getIntrduce());
            news1.setContent(news.getContent());
            news1.setTitle(news.getTitle());
            news1.setOrderCount(news.getOrderCount());
            news1.setThumbnailPath(news.getThumbnailPath());
            NewsModule newsModule = new NewsModule();
            newsModule.setId(3);
            news1.setModule(newsModule);
            newsService.save(news1);
        }

    }
}
